﻿namespace Ymatou.UserAuth
{
    using System;
    using Ymatou.User.ServerResponse;
    //using Ymatou.Authenticate.UserRegister;
    using Ymatou.User.Dto;
    using Ymatou.User.Dto.Register;
    using Ymatou.User.ServerResponse;
    using Ymatou.CommonService;
    using Ymatou.User.Common.UserEnum;
    using Ymatou.UserAuth.Agent;
    using Ymatou.UserAuth.Utility;

    /// <summary>
    /// 适配响应消息
    /// </summary>
    public static class AdapterResponse
    {

        public static ResponseData<UserRegisterResponse> AdapterUserRegResponseMessage(this ResponseData<UserRegisterInfoResponseDto> reg, string successMsg = "", string failMsg = "")
        {
            if (reg == null)
                return ResponseData<UserRegisterResponse>.CreateFail(new UserRegisterResponse { }, lastErrorMessage: "system errror");
            if (reg.Result.VerifyCode == UserLoginBillsVerifyCode.Success)
            {
                var response = new UserRegisterResponse();
                response.UserId = reg.Result.UserId;
                response.Msg = "注册成功";
                response.VerifyCode = reg.Result.VerifyCode;
                return ResponseData<UserRegisterResponse>.CreateSuccess(response, message: "注册成功");
            }
            else
            {
                var response = new UserRegisterResponse();
                response.UserId = -1;
                response.Msg = "注册失败";
                response.VerifyCode = reg.Result.VerifyCode;
                return ResponseData<UserRegisterResponse>.CreateFail(response, lastErrorMessage: GetErrorDesc(reg.Result.VerifyCode));
            }
        }

        public static ResponseData<ValidateRegEmailResponse> AdapterUserEmailVerifyMessage(this ResponseData<LoginBillsResponseDto> reg, string successMsg = "", string failMsg = "")
        {
            if (reg == null)
                return ResponseData<ValidateRegEmailResponse>.CreateFail(new ValidateRegEmailResponse { Msg = "出错啦" }, lastErrorMessage: "");
            if (reg.Success)
            {
                var response = new ValidateRegEmailResponse();
                response.Msg = successMsg;

                return Ymatou.User.ServerResponse.ResponseData<ValidateRegEmailResponse>.CreateSuccess(response, message: successMsg);
            }
            else
            {
                var response = new ValidateRegEmailResponse();
                response.Msg = failMsg;
                response.VerifyCode = reg.Result.Code;
                return ResponseData<ValidateRegEmailResponse>.CreateFail(response, lastErrorMessage: GetErrorDesc(reg.Result.Code));
            }
        }
        /// <summary>
        /// 适配用户邮箱验证从服务器返回的消息
        /// </summary>
        /// <param name="reg"></param>
        /// <returns></returns>
        public static ResponseData<ValidateRegEmailResponse> AdapterUserEmailVerifyMessage(this ResponseData<UserLoginBillsVerifyCode> reg, string successMsg = "", string failMsg = "")
        {
            if (reg == null)
                return ResponseData<ValidateRegEmailResponse>.CreateFail(new ValidateRegEmailResponse { Msg = "出错啦" }, lastErrorMessage: "");
            if (reg.Success)
            {
                var response = new ValidateRegEmailResponse();
                response.Msg = successMsg;

                return Ymatou.User.ServerResponse.ResponseData<ValidateRegEmailResponse>.CreateSuccess(response, message: "手机可以使用");
            }
            else
            {
                var response = new ValidateRegEmailResponse();
                response.Msg = failMsg;
                response.VerifyCode = reg.Result;
                return ResponseData<ValidateRegEmailResponse>.CreateFail(response, lastErrorMessage: GetErrorDesc(reg.Result));
            }
        }

        public static ResponseData<BaseResponse> AdapterUserProfileResponseMessage(this ResponseData<bool> reg, string successMsg = "", string failMsg = "")
        {
            if (reg == null) return ResponseData<BaseResponse>.CreateFail(new BaseResponse { Msg = "", VerifyCode = UserLoginBillsVerifyCode.SystemError }, lastErrorMessage: failMsg);
            if (reg.Result) return ResponseData<BaseResponse>.CreateSuccess(new BaseResponse { Msg = successMsg, VerifyCode = UserLoginBillsVerifyCode.Success }, successMsg);
            else return ResponseData<BaseResponse>.CreateFail(new BaseResponse { Msg = reg.LastErrorMessage, VerifyCode = UserLoginBillsVerifyCode.SystemError }, lastErrorMessage: failMsg);
        }
        /// <summary>
        /// 适配用户注册从服务端返回的消息
        /// </summary>
        /// <param name="reg"></param>
        /// <param name="successMsg"></param>
        /// <param name="failMsg"></param>
        /// <returns></returns>
        public static ResponseData<BaseResponse> AdapterUserBindBillsResponseMessage(this ResponseData<UserLoginBillsVerifyCode> reg, string successMsg = "", string failMsg = "")
        {
            if (reg == null)
                return ResponseData<BaseResponse>.CreateFail(new BaseResponse { Msg = "出错啦" }, lastErrorMessage: "");
            if (reg.Success)
            {
                var response = new BaseResponse();
                response.Msg = successMsg;
                response.VerifyCode = reg.Result;
                return ResponseData<BaseResponse>.CreateSuccess(response, message: successMsg);
            }
            else
            {
                var response = new BaseResponse();
                response.Msg = failMsg;
                response.VerifyCode = reg.Result;
                return ResponseData<BaseResponse>.CreateFail(response, lastErrorMessage: GetErrorDesc(reg.Result));
            }
        }

        /// <summary>
        /// 获取错误给界面显示用户看的
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        private static string GetErrorDesc(UserLoginBillsVerifyCode code)
        {
            string result = "";
            switch (code)
            {
                case UserLoginBillsVerifyCode.EmailFormartError://++
                    result = "您输入的电子邮件格式不正确";
                    break;
                case UserLoginBillsVerifyCode.EmailAlreadyExists://++
                    result = "此邮件地址已被注册，请更换或使用原电子邮件取回密码";
                    break;
                case UserLoginBillsVerifyCode.MobileFormartError:
                    result = "手机号码格式错误";
                    break;
                case UserLoginBillsVerifyCode.MobileAlreadyExists:
                    result = "手机号码已存在";
                    break;
                case UserLoginBillsVerifyCode.LoginPasswordEmpty://++
                    result = "请输入密码";
                    break;
                case UserLoginBillsVerifyCode.LoginPasswordLenthLt6://++
                    result = "密码长度应为6~16位";
                    break;
                case UserLoginBillsVerifyCode.LoginNameEmpty://++
                    result = "请填写您的昵称";
                    break;
                case UserLoginBillsVerifyCode.LoginPasswordNotMatch:
                    result = "两次输入密码不匹配";
                    break;
                case UserLoginBillsVerifyCode.LoginNameContextError://++
                    result = "只能使用中、英文、数字和下划线";
                    break;
                case UserLoginBillsVerifyCode.LoginNameFistLastError://++
                    result = "昵称首位和末位不能是下划线'_'";
                    break;
                case UserLoginBillsVerifyCode.LoginNameFullNumberError://++
                    result = "昵称不能为纯数字";
                    break;
                case UserLoginBillsVerifyCode.LoginNameAleradExists://++
                    result = "您的昵称重复，请更换";
                    break;
                case UserLoginBillsVerifyCode.LoginNameContainErrorCode://++
                    result = "您的昵称中带有非法字符";
                    break;
                case UserLoginBillsVerifyCode.UserIdError:
                    result = " 用户ID错误";
                    break;
                case UserLoginBillsVerifyCode.Success:
                    result = "合法的注册信息";
                    break;
                case UserLoginBillsVerifyCode.VerifyCodeInvalid:
                    result = "验证码无效";
                    break;
                case UserLoginBillsVerifyCode.DisagreeContract://++
                    result = "需同意协议才能注册";
                    break;
                case UserLoginBillsVerifyCode.LoginNameLengthError://++
                    result = "昵称需4~20字符";
                    break;
                case UserLoginBillsVerifyCode.EmailNullError://++
                    result = "请写邮件地址";
                    break;
                case UserLoginBillsVerifyCode.MailAndMobileIsNull:
                    result = "登录邮箱，手机都为空";
                    break;
                case UserLoginBillsVerifyCode.MailNotVerify:
                    result = "你的邮箱没有激活";
                    break;
                case UserLoginBillsVerifyCode.UserNotFind:
                    result = "登录密码错误";
                    break;
                default:
                    result = EnumHelper.GetEnumDescription(code);//没有对应的业务错误描述就默认把技术错误抛出去
                    break;
            }
            return result;
        }
    }


    #region 自定义返回的模型
    public class UserRegisterResponse
    {
        public int UserId { get; set; }
        public string Msg { get; set; }
        public UserLoginBillsVerifyCode VerifyCode { get; set; }

    }

    public class BaseResponse
    {
        public string Msg { get; set; }

        public UserLoginBillsVerifyCode VerifyCode { get; set; }
    }

    public class ValidateRegEmailResponse
    {
        public string Msg { get; set; }

        public UserLoginBillsVerifyCode VerifyCode { get; set; }
    }
    #endregion
}
